<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User_login extends CI_Controller {
	public function login(){
		$this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<span class="error help-block">', '</span>');
        $this->form_validation->set_rules('username', '用户名', 'required');
        $this->form_validation->set_rules('password', '密码', 'required|callback__auth_check');
        $this->form_validation->set_rules('verify', '验证码', 'required|callback__verify_check');
        if ($this->form_validation->run() == FALSE){
            $this->load->view('login');
        }else{
        	$username=$this->input->post('username');
        	$password=md5($this->input->post('password'));
        	$user=$this->login_model->get_one_login($username,$password);
        	$data=array(
        		'user_is_login'=>TRUE,
        	);
	        $this->session->set_userdata($data);
	        $this->session->set_userdata('username',$username);
	        $this->session->set_userdata('user_id',$user->id);
	       	redirect('/Fornt/index');
        }
	}
	public function regedit(){
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<span class="error help-block">', '</span>');
        $this->form_validation->set_rules('username', '用户名', 'required|min_length[3]|alpha_numeric|callback__username_check');
        $this->form_validation->set_rules('password', '密码', 'required|min_length[6]|alpha_dash');
        $this->form_validation->set_rules('passconf', '确认密码', 'required|callback__password_check');
        //确认密码也可以这样写，直接判断表单元素值是否相等
        //$this->form_validation->set_rules('passconf', '确认密码', 'required|matches[password]');
        $this->form_validation->set_rules('verify', '验证码', 'required|callback__verify_check');
        if ($this->form_validation->run() == FALSE){
            $this->load->view('regedit');
        }else{
        	$username=$this->input->post('username');
        	$password=md5($this->input->post('password'));
        	$this->load->model('login_model');
			$this->login_model->get_add_username($username,$password);
            redirect('Prve_admin/login');
        }
	}
	public function _username_check(){
		$username=$this->input->post('username');
		$this->load->model('login_model');
		$data=$this->login_model->get_username_check($username);
		if(!$data){
			return true;
		}else{
			$this->form_validation->set_message('_username_check', '该用户名已存在.');
			return FALSE;
		}
	}
	public function _password_check(){
		$password=$this->input->post('password');
		$passconf=$this->input->post('passconf');
		
		if($password==$passconf){
			return true;
		}else{
			$this->form_validation->set_message('_password_check', '两次输入密码不一致.');
			return FALSE;
		}
	}
	public function _auth_check(){
		$username=$this->input->post('username');
		$this->username=$username;
		$password=md5($this->input->post('password'));
		$this->load->model('login_model');
		$data=$this->login_model->get_one_login($username,$password);
		if($data){
			$this->session->set_userdata('username',$username );    //验证成功后取登录名
			return TRUE;
		}else{
			$this->form_validation->set_message('_auth_check', '用户名或密码错误.');
			return FALSE;
		}
	}

	//////////验证码的生成
	public function verify(){
		// $str = "1,2,3,4,5,6,7,8,9,a,b,c,d,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"; //要显示的字符，可自己进行增删
		// $list = explode(",", $str);
		// $count = count($list) - 1;
		// $verifyCode = '';
		// //随机生成的字符串
		// for ( $i=0; $i < 5; $i++ ){
		//       $randnum = mt_rand(0, $count);
		//       $verifyCode .= $list[$randnum];           //取出字符，组合成为我们要的验证码字符
		// }
		$verifyCode=rand(1000,9999);
		//将随机数放在session中
		$this->session->set_userdata('verify_num',$verifyCode);
		//声明需要创建的图层的图片格式
		header("Content-type: image/png");
		$width=70;        		//验证码图片的宽度
		$height=30;				//验证码图片的高度
		//创建一个图层
		$im = imagecreate($width, $height);
		//背景色
		$back = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
		$red = imagecolorallocate($im, 255, 0, 0);
		//字体色
		$font = imagecolorallocate($im, 41, 163, 238);
		$pix  = imagecolorallocate($im, 187, 230, 247);
		//干扰项，绘模糊作用的点
		mt_srand();
		for ($i = 0; $i < 1000; $i++) {
		    imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pix);
		}
		//干扰项，绘模糊作用的线
// 		for($i=0;$i<10;$i++){
// 			imagearc($im,mt_rand(0, $width), mt_rand(0, $height), 20, 20, 75, 170, $red);
// 0		}
		//输出字符
		imagestring($im, 5, 10, 8, $verifyCode, $font);
		//输出图片
		imagepng($im);
		//销毁验证码图片
		imagedestroy($im);
	}
	//验证码的校验，看输入的验证码与生成的是否一样
	public function _verify_check(){
		if($this->session->userdata('verify_num')==$this->input->post('verify')){
			return TRUE;
		}else{
			$this->form_validation->set_message('_verify_check', '验证码错误.');
			return FALSE;
		}
	}
	//销毁session值
	public function destroy(){
		$this->session->sess_destroy();
		redirect('User_login/login');
	}

}
